<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends CI_Controller {
    
    function __construct() {
        parent::__construct();
    }

    public function set_product(){
        $product_id = $this->input->post('product_id');
        if(!$product_id){
            api_failed('empty_product_id');
        }
        $sets = $this->input->post('sets');
        $this->load->model('product_model');
        $data = array('product'=>$this->product_model->set_product($product_id, $sets));
        api_success($data);
    }

    public function get_product_list() {
        $this->load->model('product_model');
        $products = $this->product_model->get_all_products();
        $products_output = array();
        foreach ($products as $product) {
            $products_output[] = stupid_product_output($product);
        }
//        print_r($products_output);
        api_success(array('product_list'=>$products_output));
    }

    public function get_product(){
        $product_id = $this->input->get('product_id', true);
        if(!$product_id || preg_match('~[^0-9]~', $product_id)){
            api_failed('invalid_product_id');
        }
        $this->load->model('product_model');
        $product = $this->product_model->get_product($product_id, null);
        if (!$product) {
            api_failed('product_not_eixsts');
        }
        api_success(array('product'=>stupid_product_output($product)));
    }
}